package com.zlx.store.goods.controller;

import com.zlx.store.common.results.HttpResp;
import com.zlx.store.common.constant.RespCode;
import com.zlx.store.domain.entity.Goods;
import com.zlx.store.domain.vo.GoodsVo;
import com.zlx.store.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * (Goods)表控制层
 *
 * @author makejava
 * @since 2023-07-29 17:11:41
 */
@RestController
@RequestMapping("/api/goods")
public class GoodsController {
    /**
     * 服务对象
     */
    @Autowired
    private GoodsService goodsService;

    /**
     * 显示商品详情
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public HttpResp queryById(Long id) {
        GoodsVo goodsVo=this.goodsService.detail(id);
        if (goodsVo.getGoods().getGoodsSaleState()!=1){
            return HttpResp.httpResp(RespCode.ITEM_OPERATION_ERROR,"商品已下架",new Date());
        }
        if (goodsVo.getGoods().getGoodsStock()<0){
            return HttpResp.httpResp(RespCode.ITEM_OPERATION_ERROR,"商品库存不足",new Date());
        }
        return HttpResp.httpResp(RespCode.ITEM_OPERATION_SUCCESS,goodsVo,new Date());
    }

    /**
     * 新增商品
     *
     * @param goods 实体
     * @return 新增结果
     */
    @PostMapping("addGoods")
    public HttpResp<Goods> add(Goods goods) {
        Goods goods1=this.goodsService.insert(goods);
        return HttpResp.httpResp(RespCode.ITEM_OPERATION_SUCCESS,goods1,new Date());
    }

    /**
     * 编辑商品数据
     *
     * @param goods 实体
     * @return 编辑结果
     */
    @PutMapping("editGoods")
    public HttpResp<Goods> edit(Goods goods) {
        Goods goods1=this.goodsService.update(goods);
        return HttpResp.httpResp(RespCode.ITEM_OPERATION_SUCCESS,goods1,new Date());
    }

    /**
     * 删除商品
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteGoods")
    public HttpResp<Boolean> deleteById(Long id) {
        Boolean result=this.goodsService.deleteById(id);
        return HttpResp.httpResp(RespCode.ITEM_OPERATION_SUCCESS,result,new Date());
    }

}

